package com.ayu.shortlink.settlement.controller;

import com.ayu.shortlink.settlement.common.convention.result.Result;
import com.ayu.shortlink.settlement.common.convention.result.Results;
import com.ayu.shortlink.settlement.dto.req.ExpansionCouponReqDTO;
import com.ayu.shortlink.settlement.dto.req.PayCommand;
import com.ayu.shortlink.settlement.dto.req.RollbackConsumeCouponReqDTO;
import com.ayu.shortlink.settlement.dto.req.UseCouponReqDTO;
import com.ayu.shortlink.settlement.dto.resp.ChoseCouponTemplateRespDTO;
import com.ayu.shortlink.settlement.idempotent.IdempotentConsume;
import com.ayu.shortlink.settlement.service.UserCouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserCouponController {

    private final UserCouponService userCouponService;

    /**
     * 选择优惠券之后 能够返回订单的金额
     * 目标：返回使用目标优惠券的信息
     */
    @GetMapping("/api/short-link/settlement/v1/user-coupon/chose-couponTemplate")
    public Result<ChoseCouponTemplateRespDTO> choseCouponTemplate(@RequestBody PayCommand requestParam){
        return Results.success(userCouponService.choseCouponTemplate(requestParam));
    }

    /**
     * 用户核销优惠券
     */
    @PutMapping("/api/short-link/settlement/v1/user-coupon/use-couponTemplate")
    @IdempotentConsume(Mark = "UserCouponController-useCouponTemplate")
    public Result<Void> useCouponTemplate(@RequestBody UseCouponReqDTO requestParam){
        userCouponService.useCouponTemplate(requestParam);
        return Results.success();
    }

    /**
     * 用户撤销核销券
     * 只允许Locking状态改变
     */
    @PutMapping("/api/short-link/settlement/v1/user-coupon/rollback-consume-coupon")
    @IdempotentConsume(Mark = "UserCouponController-rollbackConsumeCoupon")
    public Result<Void> rollbackConsumeCoupon(@RequestBody RollbackConsumeCouponReqDTO requestParam){
        userCouponService.rollbackConsumeCoupon(requestParam);
        return Results.success();
    }

    /**
     * 用户优惠券膨胀
     * 【暂时只允许神券膨胀】
     */
    @PutMapping("/api/short-link/settlement/v1/user-coupon/expansion-coupon")
    @IdempotentConsume(Mark = "UserCouponController-expansionCoupon")
    public Result<Void> expansionCoupon(@RequestBody ExpansionCouponReqDTO requestParam){
        userCouponService.expansionCoupon(requestParam);
        return Results.success();
    }
}
